package com.kaia.shoebox.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.kaia.shoebox.R;
import com.kaia.shoebox.db.dao.Importance;
import com.ptashek.widgets.datetimepicker.DateTimePicker;

public class NewTodoActivity extends Activity {

	private static final String TAG = NewTodoActivity.class.getSimpleName();

	// ------------------------------------------------------------------
	// Attributes
	// ------------------------------------------------------------------

	private EditText mTodoTitle;
	private TextView mDueDateText;
	private Button mShowDatePicker;
	private EditText mAdditionalStuff;
	private Spinner mImportanceSpinner;
	private Button mAddNewButton;
	
	// ------------------------------------------------------------------
	// System Methods
	// ------------------------------------------------------------------

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(TAG, "onCreate");

		setContentView(R.layout.new_todo);

		mTodoTitle = (EditText) findViewById(R.id.new_todo_title);

		mDueDateText = (TextView) findViewById(R.id.new_todo_due_date);

		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm a");

		mDueDateText.setText(sdf.format(now));

		mShowDatePicker = (Button) findViewById(R.id.new_todo_due_date_button);
		mShowDatePicker.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				showDateTimeDialog();
			}
		});

		mAdditionalStuff = (EditText) findViewById(R.id.new_todo_extra_info);

		mImportanceSpinner = (Spinner) findViewById(R.id.new_todo_importance_spinner);
		mImportanceSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// Do Nothing
					}

					public void onNothingSelected(AdapterView<?> parent) {
						// Do Nothing
					}
				});

		mAddNewButton = (Button) findViewById(R.id.new_todo_create_button);
		mAddNewButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				try {

					String title = mTodoTitle.getText().toString();

					String strDueDate = mDueDateText.getText().toString();

					SimpleDateFormat format = new SimpleDateFormat(
							"yyyy/MM/dd hh:mm a");
					Date dueDate = format.parse(strDueDate);

					String notes = mAdditionalStuff.getText().toString();

					Importance importance = Importance
							.getImprtanceByNumber(mImportanceSpinner
									.getSelectedItemPosition());

					if (title.equals("")) {
						Toast.makeText(NewTodoActivity.this,
								"Title, empty, please enter one",
								Toast.LENGTH_SHORT).show();
					} else {
						Intent data = new Intent();
						data.putExtra("title", title);
						data.putExtra("due", dueDate.getTime());
						data.putExtra("notes", notes);
						data.putExtra("importance",
								importance.getImportanceNumber());

						Log.i(TAG,
								"Sending result: NEW_TODO:[title=" + title
										+ ", due=" + dueDate.getTime()
										+ ", notes=" + notes + ", importance="
										+ importance.getImportanceNumber());

						setResult(RESULT_OK, data);
						finish();
					}
				} catch (ParseException e) {
					Log.e(TAG, "Date parsing error", e);
				}
			}
		});

	}
	
	// ------------------------------------------------------------------
	// Utility Methods
	// ------------------------------------------------------------------

	private void showDateTimeDialog() {
		final Dialog dateTimeDialog = new Dialog(this);
		final RelativeLayout dateTimeView = (RelativeLayout) getLayoutInflater()
				.inflate(R.layout.date_time_dialog, null);
		final DateTimePicker dateTimePicker = (DateTimePicker) dateTimeView
				.findViewById(R.id.date_time_picker);

		final String timeS = android.provider.Settings.System.getString(
				getContentResolver(),
				android.provider.Settings.System.TIME_12_24);
		final boolean is24h = !(timeS == null || timeS.equals("12"));

		((Button) dateTimeView.findViewById(R.id.set_date_time))
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						dateTimePicker.clearFocus();

						String date = dateTimePicker.get(Calendar.YEAR) + "/"
								+ (dateTimePicker.get(Calendar.MONTH) + 1)
								+ "/"
								+ dateTimePicker.get(Calendar.DAY_OF_MONTH)
								+ " ";

						StringBuilder time = new StringBuilder();

						if (dateTimePicker.is24HourView()) {

							if (dateTimePicker.get(Calendar.HOUR_OF_DAY) < 10) {
								time.append("0"
										+ dateTimePicker
												.get(Calendar.HOUR_OF_DAY));
							} else {
								time.append(dateTimePicker
										.get(Calendar.HOUR_OF_DAY));
							}

							time.append(":");

							if (dateTimePicker.get(Calendar.MINUTE) < 10) {
								time.append("0"
										+ dateTimePicker.get(Calendar.MINUTE));
							} else {
								time.append(dateTimePicker.get(Calendar.MINUTE));
							}

						} else {

							if (dateTimePicker.get(Calendar.HOUR) < 10) {
								time.append("0"
										+ dateTimePicker.get(Calendar.HOUR));
							} else {
								time.append(dateTimePicker.get(Calendar.HOUR));
							}

							time.append(":");

							if (dateTimePicker.get(Calendar.MINUTE) < 10) {
								time.append("0"
										+ dateTimePicker.get(Calendar.MINUTE));
							} else {
								time.append(dateTimePicker.get(Calendar.MINUTE));
							}

							time.append(dateTimePicker.get(Calendar.AM_PM) == Calendar.AM ? " AM"
									: " PM");
						}

						mDueDateText.setText(date + time.toString());

						dateTimeDialog.dismiss();
					}
				});

		((Button) dateTimeView.findViewById(R.id.cancel_dialog))
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						dateTimeDialog.cancel();
					}
				});

		((Button) dateTimeView.findViewById(R.id.reset_date_time))
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						dateTimePicker.reset();
					}
				});

		dateTimePicker.setIs24HourView(is24h);
		dateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dateTimeDialog.setContentView(dateTimeView);
		dateTimeDialog.show();

	}
}